/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itsc.ds2.bl.SaleBlLocal;
import mx.itsc.ds2.bl.SaleDetailBlLocal;
import mx.itsc.ds2.model.SaleDetails;
import mx.itsc.ds2.model.Sales;

/**
 *
 * @author Eli
 */
@ManagedBean
@SessionScoped
public class SalesBean {

    @EJB
    private SaleBlLocal saleBl;
    @EJB
    private SaleDetailBlLocal saleDetailBl;
    private List<Sales> listSales;
    private List<SaleDetails> listSaleDetails;
    private Sales sale;
    private int selectedSaleDetail;
    private String accion;

    public SalesBean() {
    }

    public List<Sales> getListSales() {
        listSales=saleBl.getAll();
        return listSales;
    }

    public List<SaleDetails> getListSaleDetails() {
        
        return listSaleDetails;
    }

    public Sales getSale() {
        return sale;
    }

    public void setSale(Sales sale) {
        this.sale = sale;
    }

    public int getSelectedSaleDetail() {
        return selectedSaleDetail;
    }

    public void setSelectedSaleDetail(int selectedSaleDetail) {
        this.selectedSaleDetail = selectedSaleDetail;
    }

    public String procesarSeleccion() {
        listSaleDetails = new ArrayList<SaleDetails>();
        sale.setIdsaleDetail(saleDetailBl.findById(selectedSaleDetail));//setIdDisc(discBl.findById(selectedDisc));
        return "ConfirmSale";
    }

    public String processAction() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            sale.setIdsaleDetail(saleDetailBl.findById(selectedSaleDetail));
        }
        return "ConfirmSale";
    }

    public String procesarAction() {
        saleBl.register(sale);
        accion = "";
        return "SalesList";
    }

    //listeners-----------------------------------------------------------------------
    public void prepararNuevo(ActionEvent e) {
        listSales = new ArrayList<Sales>();
        accion = "Nuevo";
        sale = new Sales();
    }

    public void agregarDetalle(ActionEvent evt) {
        if (!listSaleDetails.contains(saleDetailBl.findById(selectedSaleDetail))) {
            listSaleDetails.add(saleDetailBl.findById(selectedSaleDetail));
        } else {
        }
    }
}
